define([
    "underscore",
    "backbone",
    "pageConfig",
    "common",
    "apiPathMapping",
    "ajaxControl",
    "js/page/main/globalMenu",
    "nativeCommand"
], function (_, Backbone, pageConfig, common, link, ajax, menu, cmd) {
    'use strict';

    var current = {};
    var histroyArray = [];
    var info = {
        link: link,
        ajax: ajax,
        menu: menu,
        cmd: cmd,
        pageback: function () {
            var lastUrl = histroyArray.pop() || "";
            common.goto(lastUrl);
        }
    };

    var action = function () {
        var url = Backbone.history.fragment;
        console.log("[routes]" + url, arguments);
        var flag = url.split('/')[0];
        if (pageConfig[flag]) {
            var select = pageConfig[flag];
            var path = select.path;
            var html = "text!\/html\/" + (select.html || path) + ".html";
            var css = "text!\/css\/" + (select.css || path.replace("Page", "")) + ".css";
            require(["js\/page\/" + path, html, css], function (pageTemp, htmlTemp, cssTemp) {
                var callback = function (model) {
                    var pageInfo = {
                        url: url,
                        path: path,
                        html: htmlTemp,
                        css: cssTemp,
                        bg: select.bg || 1,
                        json: $.extend({serverTime: Date.now()}, (model || {})),
                    };
                    histroyArray.push(url);
                    if (current && current.remove) {
                        current.remove();
                    }
                    pageTemp = $.extend(pageTemp, pageInfo, info);
                    pageTemp.init();
                    pageTemp.menu = $.extend({}, menu, pageInfo, info);
                    pageTemp.menu.init();
                    current = pageTemp;
                }

                if ("needMode" in pageTemp) {
                    ajax.pageGet({
                        name: "page/" + path,
                        needMode: pageTemp.needMode
                    }, function (res) {
                        //console.log("url:" + url + "\tmodel:" + JSON.stringify(res));
                        if (res)
                            callback(res);
                        else {
                            console.log("数据访问出错：" + url);
                        }
                    });
                } else {
                    callback();
                }
            });
        } else {
            console.log("不存在的页面：" + url);
        }
    }

    return {
        current: histroyArray.pop() || "",
        action: action,
        pathSetAction: function () {
            var routes = {};
            for (var i in pageConfig) {
                routes[pageConfig[i].url] = pageConfig[i].pageInit || action;
            }
            return routes;
        },
        pathSetEvent: function (route) {
            // for (var i in base) {
            //     route.on("route:action", function (action) {
            //         console.log("action:" + action);
            //     });
            // }
        },
    };
});